from keras.models import model_from_json
import os
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import numpy as np
from PIL import Image


cleanpath='c:/data/images/clean/'
dewpath='c:/data/images/dew/'
frostpath='c:/data/images/frost/'

clean_array=np.empty((0,300,300),dtype=np.uint8)
dew_array=np.empty((0,300,300),dtype=np.uint8)
frost_array=np.empty((0,300,300),dtype=np.uint8)

for i in range(1,201):
    img_clean=Image.open(cleanpath+str(i)+'.bmp')
    data=img_clean.getdata()
    array=np.array(data,dtype=np.uint8)
    array_=np.reshape(array,(1,300,300))
    clean_array=np.row_stack((clean_array,array_))
    print("Now, clean_array's length is :"+str(len(clean_array))+"\n")

for i in range(1,201):
    img_dew=Image.open(dewpath+str(i)+'.bmp')
    data=img_dew.getdata()
    array=np.array(data,dtype=np.uint8)
    array_=np.reshape(array,(1,300,300))
    dew_array=np.row_stack((dew_array,array_))
    print("Now, dew_array's length is :"+str(len(dew_array))+"\n")

for i in range(1,201):
    img_frost=Image.open(frostpath+str(i)+'.bmp')
    data=img_frost.getdata()
    array=np.array(data,dtype=np.uint8)
    array_=np.reshape(array,(1,300,300))
    frost_array=np.row_stack((frost_array,array_))
    print("Now, frost_array's length is :"+str(len(frost_array))+"\n")

print("Now, all training data is already.\n ")

clean_label=np.empty((0,1),dtype=np.int8)
for i in range(0,200):
    clean_label=np.row_stack((clean_label,np.array([0])))

dew_label=np.empty((0,1),dtype=np.int8)
for i in range(0,200):
    dew_label=np.row_stack((dew_label,np.array([1])))

frost_label=np.empty((0,1),dtype=np.int8)
for i in range(0,200):
    frost_label=np.row_stack((frost_label,np.array([2])))

print("Now, all label data is already.\n")
print("Trying to connect all data...\n")

train_data1=np.row_stack((clean_array[0:160],dew_array[0:160],frost_array[0:160])).reshape(480,300,300,1) #80% 2400
test_data1=np.row_stack((clean_array[160:],dew_array[160:],frost_array[160:])).reshape(120,300,300,1) #20% 600

train_label1=np.row_stack((clean_label[0:160],dew_label[0:160],frost_label[0:160]))
test_label1=np.row_stack((clean_label[160:],dew_label[160:],frost_label[160:]))


train_label=keras.utils.to_categorical(train_label1,3)
test_label=keras.utils.to_categorical(test_label1,3)

train_data=train_data1.astype('float32')
test_data=test_data1.astype('float32')
train_data/=255
test_data/=255

# Here data is already.

json_file = open('nice_model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("nice_model.h5")

opt=keras.optimizers.rmsprop(lr=0.0001,decay=1e-6)
loaded_model.compile(loss='categorical_crossentropy',optimizer=opt,metrics=['accuracy'])

resault=loaded_model.predict(train_data[150].reshape(1,300,300,1),batch_size=20,verbose=1) # 150 sample
print(resault)

